package amazon

import (
	crypto_rand "crypto/rand"
	_ "embed"
	"encoding/binary"
	"fmt"
	math_rand "math/rand"
	"net/url"
	"os"
	"strings"
)

////go:embed ua
//var uaList string
//
//func GetRandomUa() string {
//	ua := strings.Split(uaList, "\r\n")
//	return ua[rand.Intn(len(ua))]
//}

func RandomInt(start int, end int) int {
	var b [8]byte
	_, err := crypto_rand.Read(b[:])
	if err != nil {
		panic("cannot seed math/rand package with cryptographically secure random number generator")
	}
	math_rand.Seed(int64(binary.LittleEndian.Uint64(b[:])))

	random := math_rand.Intn(end - start)
	random = start + random
	return random
}

func Choice(values []string) string {
	//math_rand.Seed(time.Now().Unix()) // initialize global pseudo random generator
	var b [8]byte
	_, err := crypto_rand.Read(b[:])
	if err != nil {
		panic("cannot seed math/rand package with cryptographically secure random number generator")
	}
	math_rand.Seed(int64(binary.LittleEndian.Uint64(b[:])))
	return values[math_rand.Intn(len(values))]
}

func WriteFile(filename, text string) {
	f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
	if err != nil {
		panic(err)
	}

	defer f.Close()

	if _, err = f.WriteString(fmt.Sprintf("%s", text)); err != nil {
		panic(err)
	}
}

func GetCodeAndTld(uri string) ([]string, string) {
	parseBaseUrl, _ := url.Parse(uri)
	code := strings.Split(parseBaseUrl.Hostname(), ".")
	tld := strings.Join(code[1:], ".")
	return code, tld
}
